package com.zx.cashiersystem.device;

import android.content.Context;

import com.landicorp.android.eptapi.device.Printer;
import com.landicorp.android.eptapi.device.Printer.Format;
import com.landicorp.android.eptapi.exception.RequestException;
import com.landicorp.android.eptapi.utils.QrCode;

/**
 * Created by Administrator on 2017/2/14.
 */

public abstract class PrinterSample extends AbstractSample {

    //构造打印过程
    private Printer.Progress progress = new Printer.Progress() {
        @Override
        public void doPrint(Printer printer) throws Exception {
            Format format = new Format();
            // 西文字体 5x7 点阵大小
            format.setAscSize(Format.ASC_DOT5x7);
            // 西文字体 缩放样式 1 倍宽 2 倍高
            format.setAscScale(Format.ASC_SC1x2);
            printer.setFormat(format);
            // 打印字符 一次行打印不超过 106 字符
            printer.printText("收银系统\n");
            format.setAscScale(Format.ASC_SC1x1);
            printer.setFormat(format);
            // 打印文字（加换行）
            printer.println("ASC_DOT5x7 & ASC_SC1x1");
            // 开启下划线
            printer.startUnderline();
            printer.println(10, "下划线，偏移 10");
            printer.println(20, "下划线，偏移 20");
            // 结束下划线
            printer.endUnderline();
            // 汉文字体 缩放样式 1 倍宽 1 倍高
            format.setHzScale(Format.HZ_SC1x1);
            // 汉文字体 16x16 点阵大小
            format.setHzSize(Format.HZ_DOT16x16);
            printer.setFormat(format);
            printer.printText("汉文字体 缩放 1x1 点阵 16x16\n");
            //offset - 偏移位置
            //qrCode - 二维码数据
            //imageHeght - 期望的图像高度
            printer.printQrCode(0, new QrCode("chaiyingmin", QrCode.ECLEVEL_Q), 100);

            // 按行数进退纸
            printer.feedLine(3);

        }

        @Override
        public void onFinish(int code) {
            //打印结束
            /**
             * The result is fine.
             */
            if (code == Printer.ERROR_NONE) {
                displayPrinterInfo("PRINT SUCCESS END ");
            }
            /**
             * Has some error. Here is display it, but you may want to hanle
             * the error such as ERROR_OVERHEAT   ERROR_BUSY   ERROR_PAPERENDED
             * to start again in the right time later.
             */
            else {
                displayPrinterInfo("PRINT ERROR - " + getErrorDescription(code));
            }
        }

        @Override
        public void onCrash() {
            //设备服务崩溃处理
            onDeviceServiceCrash();
        }

        public String getErrorDescription(int code) {
            switch (code) {
                case Printer.ERROR_PAPERENDED:
                    //缺纸，不能打印
                    return "Paper-out, the operation is invalid this time";
                case Printer.ERROR_HARDERR:
                    //硬件错误
                    return "Hardware fault, can not find HP signal";
                case Printer.ERROR_OVERHEAT:
                    //打印头过热
                    return "Overheat";
                case Printer.ERROR_BUFOVERFLOW:
                    //缓冲模式下所操作的位置超出范围
                    return "The operation buffer mode position is out of range";
                case Printer.ERROR_LOWVOL:
                    //低压保护
                    return "Low voltage protect";
                case Printer.ERROR_PAPERENDING:
                    //纸张将要用尽，还允许打印(单步进针打特有返回值)
                    return "Paper-out, permit the latter operation";
                case Printer.ERROR_MOTORERR:
                    // 打印机芯 故障(过快或者过慢)
                    return "The printer core fault (too fast or too slow)";
                case Printer.ERROR_PENOFOUND:
                    //自动定位没有找到对齐位置,纸张回到原来位置
                    return "Automatic positioning did not find the alignment position, the paper back to its original position";
                case Printer.ERROR_PAPERJAM:
                    //卡纸
                    return "paper got jammed";
                case Printer.ERROR_NOBM:
                    // 没有找到黑标
                    return "Black mark not found";
                case Printer.ERROR_BUSY:
                    //打印机处于忙状态
                    return "The printer is busy";
                case Printer.ERROR_BMBLACK:
                    // 黑标探测器检测到黑色信号
                    return "Black label detection to black signal";
                case Printer.ERROR_WORKON:
                    //打印机电源处于打开状态
                    return "The printer power is open";
                case Printer.ERROR_LIFTHEAD:
                    //打印头抬起(自助热敏打印机特有返回值)
                    return "Printer head lift";
                case Printer.ERROR_LOWTEMP:
                    //低温保护或AD 出错(自助热敏打印机特有返回值)
                    return "Low temperature protect";
            }
            return "unknown error (" + code + ")";
        }

    };


    public void startPrint() {
        try {
            progress.start();
        } catch (RequestException e) {
            e.printStackTrace();
            onDeviceServiceCrash();
        }
    }

    /**
     * Display printer info
     *
     * @param info
     */
    protected abstract void displayPrinterInfo(String info);

    public PrinterSample(Context context) {
        super(context);
        // Init print progress. You can also do it in 'Progress.doPrint' method,
        // but it would not be done after all step invoked.
        progress.addStep(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                // Make the print method can print more than one line.
                printer.setAutoTrunc(true);
                // Default mode is real mode, now set it to virtual mode.
                printer.setMode(Printer.MODE_VIRTUAL);
            }
        });
    }

    /**
     * 添加步骤。
     * 一般应用程序很少用到该接口，除非是对 Printer 进行封装。
     * doPrint打印的都是固定的，添加步骤添加其他信息
     */
    public void addPrintInfo(final String totalPrice) {

        progress.addStep(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                printer.setFormat(Format.asc(Format.ASC_DOT24x12, Format.ASC_SC1x2));
                printer.startUnderline();
                printer.printMid(totalPrice);
                printer.printText("\n");
            }
        });
    }


    @Override
    protected void onDeviceServiceCrash() {

    }
}
